﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Entity.NRBZ;
using Rison.BusinessFacade.Facade.NRBZ;
using Rison.DataAccess.Common;

namespace Rison.Web.NRBZ
{
    public partial class ZfgkDetail : NrfbPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void LoadInformation(DataRow dr)
        {
            if (dr != null)
            {
               
                string _gwxxNum = dr["gwxxNum"].ToString();
                SetTextBoxValue(_gwxxNum, "gwxxNum");

                string _zfxxNum = dr["zfxxNum"].ToString();
                SetTextBoxValue(_zfxxNum, "zfxxNum");

                string _gkjNum = dr["gkjNum"].ToString();
                SetTextBoxValue(_gkjNum, "gkjNum");

                //string _lmzfxx = dr["lmzfxx"].ToString();
                //SetCheckBoxValue(_lmzfxx, this.cbllmzfxx);

                //string _Ifbmgkml = dr["Ifbmgkml"].ToString();
                //rblIfbmgkml.SelectedValue = (_Ifbmgkml == "是") ? _Ifbmgkml : "否";

                string _Ifxsqsydw = dr["Ifxsqsydw"].ToString().Trim();
                rblIfxsqsydw.SelectedValue = (_Ifxsqsydw == "是") ? _Ifxsqsydw : "否";

                string _Ifqyly = dr["Ifqyly"].ToString();
                SetCheckBoxValue(_Ifqyly, this.cblIfqyly);

                //string _Ifqygkml = dr["Ifqygkml"].ToString();
                //rblIfqygkml.SelectedValue = (_Ifqygkml == "是") ? _Ifqygkml : "否";

                NrbzFacade facade = new NrbzFacade();
                
                DataSet ds = facade.LoadAttachement(NrbzId, 6);
                DataList6.DataSource = ds.Tables[0];
                DataList6.DataBind();

                ds = facade.LoadAttachement(NrbzId, 7);
                DataList7.DataSource = ds.Tables[0];
                DataList7.DataBind();

                ds = facade.LoadAttachement(NrbzId, 8);
                DataList8.DataSource = ds.Tables[0];
                DataList8.DataBind();

                //ds = facade.LoadAttachement(NrbzId, 3);
                //DataList3.DataSource = ds.Tables[0];
               // DataList3.DataBind();
                 
            }

            //htmlTable
            //LoadHtmlTable(NrbzSubType.NrbzSubType21.ToString(), "ZfgkDetail1_input_5_1", "table5_1",1);
            //LoadHtmlTable(NrbzSubType.NrbzSubType22.ToString(), "ZfgkDetail1_input_5_2", "table5_2",1);
            //LoadHtmlTable(NrbzSubType.NrbzSubType23.ToString(), "ZfgkDetail1_input_5_3", "table5_3",1);
        }

        public override long SaveInformation()
        {
            NrbzFacade facade = new NrbzFacade();
            List<DataField> l = new List<DataField>();            
            
            int returnValue = 0;
            string returnSubValue = "";

            l.Add(new DataField("gwxxNum",GetTextBoxValue("gwxxNum", 6)));
            l.Add(new DataField("zfxxNum", GetTextBoxValue("zfxxNum", 6)));
            l.Add(new DataField("gkjNum", GetGkjNum()));
            //l.Add(new DataField("lmzfxx",GetCheckBoxValue(this.cbllmzfxx)));
            //l.Add(new DataField("Ifbmgkml", rblIfbmgkml.SelectedValue));
            l.Add(new DataField("Ifxsqsydw", rblIfxsqsydw.SelectedValue));
            l.Add(new DataField("Ifqyly", GetCheckBoxValue(this.cblIfqyly)));
            //l.Add(new DataField("Ifqygkml", rblIfqygkml.SelectedValue));
            returnValue = facade.updateValue(l, NrbzId);

            //htmlTable
           
            //returnSubValue = SaveHtmlTabel("ZfgkDetail1_input_5_1", NrbzSubType.NrbzSubType21.ToString(), 1);

            returnSubValue = SaveHtmlTabel("ZfgkDetail1_input_5_2", NrbzSubType.NrbzSubType22.ToString(), 1);
            //returnSubValue = SaveHtmlTabel("ZfgkDetail1_input_5_3", NrbzSubType.NrbzSubType23.ToString(), 1);

            uploadFile();

            return returnValue;
        }
        protected string GetGkjNum()
        {
            int t2 = 0;
            try
            {
                t2 = int.Parse(this.txtgkjNum2.Text);
            }
            catch (Exception e)
            {
                t2 = 0;
            }

            int t3 = 0;
            try
            {
                t3 = int.Parse(this.txtgkjNum3.Text);
            }
            catch (Exception e)
            {
                t3 = 0;
            }

            int t4 = 0;
            try
            {
                t4 = int.Parse(this.txtgkjNum4.Text);
            }
            catch (Exception e)
            {
                t4 = 0;
            }

            int t5 = 0;
            try
            {
                t5 = int.Parse(this.txtgkjNum5.Text);
            }
            catch (Exception e)
            {
                t5 = 0;
            }

            int t1 = t2 + t3 + t4 + t5;
            return String.Format("{0},{1},{2},{3},{4}", t1,t2,t3,t4,t5);
        }
        #region "textbox"
        protected void SetTextBoxValue(string value, string field)
        {
            string[] s = value.Split(',');
            for (int i = 0; i < s.Length; i++)
            {
                ((TextBox)this.FindControl("txt" + field + (i + 1).ToString())).Text = s[i].ToString();
            }        
        }

        protected string GetTextBoxValue(string field,int num)
        {
            StringBuilder s = new StringBuilder();
            for (int i = 0; i < num; i++)
            {
               if(i== num-1)
               {
                   s.AppendFormat("{0}", ((TextBox)this.FindControl("txt" + field + (i + 1).ToString())).Text);
               }
                else
               {
                   s.AppendFormat("{0},", ((TextBox)this.FindControl("txt" + field + (i + 1).ToString())).Text);
               }
            }
            return s.ToString();
        }
        #endregion

        //#region "RadioBox"
        //#endregion

        #region "checkbox"
        protected void SetCheckBoxValue(string value, CheckBoxList c)
        {            
            string[] s = value.Split(',');
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i].ToLower() == "true")
                    c.Items[i].Selected = true;
                else
                    c.Items[i].Selected = false;
            }
            
        }

        protected string GetCheckBoxValue(CheckBoxList c)
        {
            StringBuilder s = new StringBuilder();
            for (int i = 0; i < c.Items.Count; i++)
            {
                if (i == c.Items.Count -1 )
                    s.AppendFormat("{0}", c.Items[i].Selected.ToString().Trim());
                else
                    s.AppendFormat("{0},",c.Items[i].Selected.ToString().Trim());
            }

            return s.ToString();
        }
        #endregion

        #region "子网站 create/delete/update htmlTable"
        /// <summary>
        /// 
        /// </summary>
        /// <param name="facade"></param>
        /// <param name="field">ZfgkDetail1_input_5_1</param>
        /// <returns></returns>
        private string SaveHtmlTabel(string field,string Type,int fieldNum)
        {
            string info = HttpContext.Current.Request.Form[field];

            if (!string.IsNullOrEmpty(info))
            {
                NrbzFacade facade = new NrbzFacade();
                string msg = facade.SaveSubInfobyParentId(NrbzId, Type, fieldNum, info);
                return msg;
            }
            return "";

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="facade"></param>
        /// <param name="tablename">ZfgkDetail1_table5_1</param>
        /// <param name="fieldname">ZfgkDetail1_input_5_1</param>
        private void LoadHtmlTable(string Type, string fieldname, string tablename,int fieldNum)
        {
            NrbzFacade facade = new NrbzFacade();
            DataTable dt = facade.LoadSubInfobyParentId(NrbzId,Type,fieldNum);
            if (dt == null || dt.Rows.Count == 0)
            {
                DrawOneRow(tablename, fieldname, ""); //blank row                
            }
            else
            {

                foreach (DataRow dr in dt.Rows)
                {
                    DrawOneRow(tablename, fieldname, dr[0].ToString());
                }

            }
        }

        private void DrawOneRow(string t, string inputname, string p1)
        {

            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell ce = new HtmlTableCell();

            ce.InnerHtml = "<input type='text' name='" + inputname + "' value='" + p1 + "' style='width:191px;' class='Text'/>";
            tr.Cells.Add(ce);

            ce = new HtmlTableCell();
            ce.InnerHtml = "<img alt='删除' src='../style/images/_-.gif' onclick='removeParentNode(this);' class='img'/>";
            tr.Cells.Add(ce);

            ((HtmlTable)this.FindControl(t)).Rows.Add(tr);
        }
        #endregion

        #region "upload"
        public string uploadFile()
        {
            string val = Request.Params["fileUploader6_hdnFileVal"];
            string val1 = Request.Params["fileUploader7_hdnFileVal"];
            string val2 = Request.Params["fileUploader8_hdnFileVal"];
            List<SysAttachment> _attachments = new List<SysAttachment>();
            int returnValue = -1;
            string del = "";

            NrbzFacade facade = new NrbzFacade();

            //附件6
            if (!string.IsNullOrEmpty(val))
            {
                _attachments = NewUpload(val);
                returnValue = facade.InsertAttachment(NrbzId, 6, _attachments);
            }

            del = Request.Params["fileUploader6_del"];
            if (!string.IsNullOrEmpty(del))
                returnValue = facade.DeleteAttachment(del);

            //附件7
            if (!string.IsNullOrEmpty(val1))
            {
                _attachments = NewUpload(val1);
                returnValue = facade.InsertAttachment(NrbzId, 7, _attachments);
            }

            del = Request.Params["fileUploader7_del"];
            if (!string.IsNullOrEmpty(del))
                returnValue = facade.DeleteAttachment(del);

            //附件8
            if (!string.IsNullOrEmpty(val2))
            {
                _attachments = NewUpload(val2);
                returnValue = facade.InsertAttachment(NrbzId, 8, _attachments);
            }

            del = Request.Params["fileUploader8_del"];
            if (!string.IsNullOrEmpty(del))
                returnValue = facade.DeleteAttachment(del);

            if (returnValue < 0)
            {
                return "文件上传成功，保存有误！";
            }

            return "";
        }
        #endregion
    }
}